package com.sunshine.androidjavapractise.ui.activity;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.OpenableColumns;
import android.provider.UserDictionary;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import com.sunshine.androidjavapractise.R;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Random;

public class HttpTestActivity extends AppCompatActivity {
    private Button btnTest;
    private Button tvResult;
    private Random mRandom = new Random(999999999);
    private static final String BASE_URL = "https://pay.95516.com";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_http_test);
        initView();
    }

    private void initView() {
        btnTest = (Button) findViewById(R.id.btn_test);
        tvResult = (Button) findViewById(R.id.tv_result);
        btnTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestFile();
            }
        });
        tvResult.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPhotoAndCrop();
            }
        });
    }

    private void requestFile() {
        Intent requestFile = new Intent(Intent.ACTION_PICK);
        requestFile.setType("image/jpg");
        startActivityForResult(requestFile, 11);
        String[] mProjection = {
                UserDictionary.Words._ID,
                UserDictionary.Words.WORD,
                UserDictionary.Words.LOCALE
        };
    }


    private void GetandSaveCurrentImage() {
        // 1.构建Bitmap
        WindowManager windowManager = getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        View decorview = this.getWindow().getDecorView();
        decorview.setDrawingCacheEnabled(true);
        Bitmap Bmp = decorview.getDrawingCache();
        String SavePath = getSDCardPath() + "/AndyDemo/ScreenImage";
        // 3.保存Bitmap
        try {
            File path = new File(SavePath);
            // 文件
            String filepath = SavePath + "/Screen_1.png";
            File file = new File(filepath);
            if (!path.exists()) {
                path.mkdirs();
            }
            if (!file.exists()) {
                file.createNewFile();
            }
            FileOutputStream fos = null;
            fos = new FileOutputStream(file);
            if (null != fos) {
                Bmp.compress(Bitmap.CompressFormat.PNG, 90, fos);
                fos.flush();
                fos.close();
                Toast.makeText(this, "截屏文件已保存至SDCard/AndyDemo/ScreenImage/",
                        Toast.LENGTH_LONG).show();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String getSDCardPath() {
        File sdcardDir = null;
        // 判断SDCard是否存在
        boolean sdcardExist = Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED);
        if (sdcardExist) {
            sdcardDir = Environment.getExternalStorageDirectory();
        }
        return sdcardDir.toString();
    }

    private void getPhotoAndCrop() {
        String sdcarStr = getSDCardPath();
        if (TextUtils.isEmpty(sdcarStr)) {

        } else {
            String parentPath = sdcarStr + "/AndyDemo/ScreenImage";
            File file = new File(parentPath, "Screen_1.png");
            if (file.exists()) {
                Intent intent = new Intent("com.android.camera.action.CROP");
                Uri photoUri = FileProvider.getUriForFile(
                        this,
                        getPackageName() + ".fileprovider",
                        file);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                intent.putExtra("crop", "true");
                intent.putExtra("aspectX", 1);
                intent.putExtra("aspectY", 1);
                intent.putExtra("outputX", 300);
                intent.putExtra("outputY", 300);
                intent.putExtra("scale", true);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
                intent.putExtra("return-data", false);
                intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
                intent.putExtra("noFaceDetection", true); // no face detection
                startActivityForResult(intent, 111);
            }
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        System.out.println("onActivityResult requestCode = " + requestCode + " resultCode = " + resultCode);
        if (resultCode == RESULT_OK) {
            if (requestCode == 111) {
                System.out.println("onActivityResult ");
            } else if (requestCode == 11) {
                Uri returnUri = data.getData();
                String mimeType = getContentResolver().getType(returnUri);
                System.out.println("onActivityResult mimeType " + mimeType);
                Cursor cursor = getContentResolver().query(returnUri, null, null, null, null);
                int nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
                int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
                cursor.moveToFirst();
                String name = cursor.getString(nameIndex);
                long size = cursor.getLong(sizeIndex);
                System.out.println("name = " + name + " size = " + size);
            }
        }
    }
}


